package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.model.TbUser;
import com.dongdongshop.model.TbUserExample;
import com.dongdongshop.service.UserService;
import com.dongdongshop.util.ShiroUtils;
import com.dongdongshop.vo.UserVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TbUserMapper userMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private SimpleMailMessage simpleMailMessage;


    @Override
    public Result register(UserVO userVO) {
        //注册信息
        String code = userVO.getCode();
        String redisCode = (String) redisTemplate.boundHashOps("REGISTER_CODE").get(userVO.getPhone());
        if (!code.equals(redisCode)) {
            return Result.RESPOSE(ResultEnum.REGISTER_CODE);
        }
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(userVO, tbUser);
        String salt = ShiroUtils.generateSalt(6);
        String password = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 3);
        tbUser.setSalt(salt);
        tbUser.setPassword(password);
        tbUser.setStatus("Y");
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int i = userMapper.insertSelective(tbUser);
        if (i >= 1) {
            return Result.OK();
        }
        return Result.FAIL();
    }

    @Override
    public Result registerWeibo(UserVO userVO) {
        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(userVO, tbUser);
        String salt = ShiroUtils.generateSalt(6);
        String password = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), salt, 3);
        tbUser.setSalt(salt);
        tbUser.setPassword(password);
        tbUser.setStatus("Y");
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int i = userMapper.insertSelective(tbUser);
        if (i >= 1) {
            return Result.OK();
        }
        return Result.FAIL();
    }

    @Override
    public void sendPhoneMessage(String phoneNum) {
        Map<String, Object> map = new HashMap<>();
        map.put("phoneNum", phoneNum);
        //使用mq生产者生产消息
        rocketMQTemplate.convertAndSend("dk2207-topic", JSON.toJSON(map));
    }

    @Override
    public UserVO getUserByName(String userName) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(userName);
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        if (tbUsers != null && tbUsers.size() != 0) {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(tbUsers.get(0), vo);
            return vo;
        }
        return null;
    }

    @Override
    public void postMail() {
        //先查询所有的用户//2.东东商城注册功能,当用户注册满30天并且已经连续7天没有登陆了,发一封邮件
        List<TbUser> userList = userMapper.selectByExample(null);
        List<TbUser> userList2 = new ArrayList<>();
        for (TbUser user : userList) {
            //判断用户是否注册满30天
            long l = System.currentTimeMillis();
            long createdTime = user.getCreated().getTime();
            long day1 = (l - createdTime) / 1000 / 60 / 60 / 24;
            long lastLoginTime = user.getLastLoginTime().getTime();
            long day2 = (l - lastLoginTime) / 1000 / 60 / 60 / 24;
            if (day1 >= 30 && day2 >= 7) {
                userList2.add(user);
            }
        }
        if (userList2 == null) {
            return;
        }
        //如果有值,则去发送邮件
        for (TbUser user : userList2) {
            String email = user.getEmail();
            simpleMailMessage.setTo(email);
            simpleMailMessage.setText("Hello,World");
            javaMailSender.send(simpleMailMessage);
        }
    }

}
